//
//  ZZSXImagesBrowsersView.m
//  掌中陕西
//
//  Created by Kratos on 14-8-6.
//  Copyright (c) 2014年 RM. All rights reserved.
//

#import "ZZSXImagesBrowsersView.h"
#import "CHPhotoView.h"
#import <SwipeView/SwipeView.h>

#define kPadding 0.f

@interface ZZSXPhotoView : UIView
@property (nonatomic , strong) CHPhotoView *photoView;
@end

@implementation ZZSXPhotoView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];
        
        self.photoView = [[CHPhotoView alloc] initWithFrame:CGRectMake(kPadding, 0, CGRectGetWidth(self.bounds) - kPadding * 2, CGRectGetHeight(self.bounds))];
        
        [self addSubview:self.photoView];
    }
    return self;
}


@end

@interface ZZSXImagesBrowsersView () <SwipeViewDataSource,SwipeViewDelegate,UIGestureRecognizerDelegate>
@property (nonatomic , strong)  SwipeView *swipeView;
@property (nonatomic , strong)  NSMutableArray *allImageUrls;
@property (nonatomic , strong)  NSMutableArray *allImages;
@property (nonatomic , strong)  NSString *placeholderImageName;
@property (nonatomic , assign)  BOOL showLocaImage;
@property (nonatomic , assign)  NSInteger nowIndex;
@end

@implementation ZZSXImagesBrowsersView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor blackColor]];
        self.nowIndex = 0;
        
        self.allImageUrls = [NSMutableArray array];
        self.allImages = [NSMutableArray array];
        
        self.swipeView = [[SwipeView alloc]initWithFrame:self.bounds];
        self.swipeView.delegate = self;
        self.swipeView.dataSource = self;
        
        [self addSubview:self.swipeView];
        
    }
    return self;
}

-(void)setImageUrls:(NSArray *)imageUrls placeholderImage:(UIImage *)placeholder
{
    
    self.showLocaImage = NO;
    [self.allImageUrls removeAllObjects];
    [self.allImageUrls addObjectsFromArray:imageUrls];
    
    self.placeholderImageName = [placeholder copy];
    
    [self.swipeView reloadData];
}

-(void)setImages:(NSArray *)images
{
    self.showLocaImage = YES;
    [self.allImages removeAllObjects];
    [self.allImages addObjectsFromArray:images];
    
    [self.swipeView reloadData];

}

#pragma mark - SwipeViewDataSource
- (NSInteger)numberOfItemsInSwipeView:(SwipeView *)swipeView
{
    return self.showLocaImage ? [self.allImages count] : [self.allImageUrls count];
}

- (UIView *)swipeView:(SwipeView *)swipeView viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    ZZSXPhotoView *photoView = (ZZSXPhotoView *)view;
    if (!photoView) {
       photoView = [[ZZSXPhotoView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(swipeView.bounds), CGRectGetHeight(swipeView.bounds))];
        
        UITapGestureRecognizer *signalTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewSignalTap:)];
        signalTap.delegate = self;
        signalTap.numberOfTapsRequired = 1;
        signalTap.numberOfTouchesRequired = 1;
        [photoView addGestureRecognizer:signalTap];
        
        view = photoView;
    }
    
    [photoView.photoView resetZoom];
    
    if (self.showLocaImage) {
       [photoView.photoView.imageView setImage:[UIImage imageNamed:self.allImages[index]]];
    }
    else
    {
        NSURL *imageUrl = [NSURL URLWithString:self.allImageUrls[index]];
        [photoView.photoView.imageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:self.placeholderImageName]];
    }
    
    return view;
}

#pragma mark - SwipeViewDelegate
- (void)swipeViewCurrentItemIndexDidChange:(SwipeView *)swipeView
{
    self.nowIndex = swipeView.currentItemIndex;
    if ([_delegate respondsToSelector:@selector(browsersCurrentItemIndexDidChange:)]) {
        [_delegate browsersCurrentItemIndexDidChange:swipeView.currentItemIndex];
    }
}

- (void)swipeView:(SwipeView *)swipeView didSelectItemAtIndex:(NSInteger)index
{
    self.nowIndex = index;
    if ([_delegate respondsToSelector:@selector(browsersDidSelectItemAtIndex:)]) {
        [_delegate browsersDidSelectItemAtIndex:index];
    }
}

-(void)viewSignalTap:(UITapGestureRecognizer*)recognizer
{
    if ([_delegate respondsToSelector:@selector(browsersDidSelectItemAtIndex:)]) {
        [_delegate browsersDidSelectItemAtIndex:self.nowIndex];
    }
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 判断是不是UIButton的类
    if ([touch.view isKindOfClass:[UIButton class]])
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

/**
 * 保存图片
 * @param sender
 * @return
 * @author Kratos
 */
- (void)saveImage
{
    ZZSXPhotoView *photoView = (ZZSXPhotoView *)[self.swipeView itemViewAtIndex:self.nowIndex];
    UIImage *image = photoView.photoView.imageView.image;
    if (image) {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        //@"图片已保存到相册"
    }
    else
    {
        //@"图片保存失败"
    }

}

@end
